package com.recruit.g1.job.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.job.mapper.JobMapper;
import com.recruit.g1.job.service.InterestedJobsService;
import com.recruit.g1.pojo.job.dto.JobInsertParam;
import com.recruit.g1.pojo.job.dto.JobListQuery;
import com.recruit.g1.pojo.job.dto.JobUpdateDTO;
import com.recruit.g1.pojo.job.entity.Job;
import com.recruit.g1.pojo.job.vo.JobListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "岗位管理模块")
@RestController
@RequestMapping("/v1/job")
public class JobController {

    @Autowired
    private JobMapper jobMapper;
    @GetMapping("/count/")
    @ApiOperation("岗位数量")
    public JsonResult count() {
        int count = jobMapper.selectCount();
        return JsonResult.ok(count);
    }

    @PostMapping("/updateInfo")
    @ApiOperation("更新岗位")
    public JsonResult updateInfo(@RequestBody JobUpdateDTO jobUpdateDTO) {
        log.debug("更新岗位业务参数 ={} " +jobUpdateDTO);
        Job job = new Job();

        BeanUtils.copyProperties(jobUpdateDTO,job);
        jobMapper.updateJob(job);
        return JsonResult.ok(job);
    }

    @GetMapping("select/{id}")
    @ApiOperation("查询岗位信息")
    public JsonResult select(@PathVariable Integer id) {
        log.debug("id = " + id);
        Job job = jobMapper.selectById(id);
        return JsonResult.ok(job);
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除岗位")
    public JsonResult delete(@PathVariable Integer id) {
        log.debug("id = " + id);
        jobMapper.deleteJob(id);
        return JsonResult.ok();
    }
    @PostMapping("/update/{id}/{status}")
    @ApiOperation("更新岗位上下架")
    public JsonResult updateJobStatus(@PathVariable Integer id,@PathVariable Integer status){
        log.debug("更新岗位上下架业务参数 ={} " +id);

        Job job = new Job();
        job.setId(id);

        if (status == 1) {
            job.setStatus(0);
        } else {
            job.setStatus(1);
        }

        job.setUpdateTime(new Date());
        jobMapper.updateJobStatusById(job);
        return JsonResult.ok();
    }

    @PostMapping("/save")
    @ApiOperation("发布岗位")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(JobInsertParam jobInsertParam) {
        log.debug("新增岗位业务参数 ={} " + jobInsertParam);
        //登陆状态
        //UserSaveParam userSaveParam = (UserSaveParam) session.getAttribute(session.getId());
        /*User userSaveParam =(User) session.getAttribute("currentLoginUser");

        if (userSaveParam == null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }*/
        Job job = new Job();
        BeanUtils.copyProperties(jobInsertParam, job);
        job.setCompanyId(1);
        job.setStatus(1);
        job.setCreateTime(new Date());
        jobMapper.insertJob(job);
        return JsonResult.ok("发布成功");
    }


    @GetMapping("list/{status}")
    @ApiOperation("查询岗位列表")
    @ApiOperationSupport(order = 20)
    public JsonResult select(@PathVariable Integer status,
                             @RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "10") int pageSize) {

        // 构造查询参数对象
        JobListQuery jobListQuery = new JobListQuery();
        jobListQuery.setStatus(status);
        jobListQuery.setPageNum(pageNum);
        jobListQuery.setPageSize(pageSize);

        PageHelper.startPage(pageNum, pageSize);
        List<JobListVO> list = jobMapper.selectjobList(jobListQuery);  // ✅ 正确传入封装对象
        log.info("【查询结果】{}", list); // 🔍 添加日志输出
        PageInfo<JobListVO> pageInfo = new PageInfo<>(list);

        return JsonResult.ok(pageInfo);
    }


//    @PostMapping("delete/{id}")
//    @ApiOperation("删除岗位")
//    @ApiOperationSupport(order = 30)
//    public JsonResult delete(@PathVariable Integer id, @ApiIgnore HttpSession session) {
//        log.debug("id = " + id);
//
//        //UserSaveParam userSaveParam = (UserSaveParam) session.getAttribute(session.getId());
//        /*User userSaveParam =(User) session.getAttribute("currentLoginUser");
//
//        if (userSaveParam == null){
//            return JsonResult.fail(StateCode.NOT_LOGIN);
//        }*/
//
//        // 检查id是否为null
//        if (id == null) {
//            return JsonResult.error("ID cannot be null");
//        }
//
//        jobMapper.deleteJob(id);
//
//
//        return JsonResult.ok("删除成功");
//    }






















/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





    @Autowired
    InterestedJobsService interestedJobsService;
    @GetMapping("/select/{userId}")
    @ApiOperation("查询感兴趣岗位")
    @ApiOperationSupport(order = 10)
    public JsonResult selectInterestedJobs(@PathVariable Long userId){
        log.debug("传过来的userID的值为{}",userId);
        List<Job> list = interestedJobsService.selectInterestedJobs(userId);
        return JsonResult.ok(list);

    }
    @GetMapping("/selectJob/{userId}/{jobId}")
    @ApiOperation("查询具体感兴趣岗位")
    @ApiOperationSupport(order = 20)
    public JsonResult selectInterestedJob(@PathVariable Long userId,@PathVariable Long jobId){
        log.debug("传过来的userID的值为"+userId+"传过来的jobId的值为"+jobId);
        List<Job> list = interestedJobsService.selectInterestedJob(userId, jobId);
        return JsonResult.ok(list);

    }
//    @PostMapping("/delete/{jobId}")
//    @ApiOperation("取消关注感兴趣岗位")
//    @ApiOperationSupport(order = 30)//本方法因为没有处理user模块登录成功，本模块获取不到登录用户的问题，所以本方法目前还无法使用
//    public JsonResult deleteInterestedJobs(@PathVariable Long jobId, HttpSession session){
//        interestedJobsService.deleteInterestedJobs(jobId,session);
//        return JsonResult.ok();
//    }










}
